<!-- 游戏编辑弹窗 -->
<template>
  <el-dialog
    :title="isUpdate ? '修改收款账户' : '添加收款账户'"
    :visible="visible"
    width="1000px"
    :destroy-on-close="true"
    :lock-scroll="false"
    @update:visible="updateVisible"
    append-to-body
    :close-on-click-modal="false"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="150px">
      <el-form-item label="公司名称:" prop="company">
        <el-input
          v-model="form.company"
          placeholder="请输入公司名称"
          clearable
        />
      </el-form-item>
      <el-form-item label="收款通道:" prop="pay_way_id">
        <el-select
          :disabled="isUpdate"
          :placeholder="'请选择收款通道'"
          v-model="form.pay_way_id"
        >
          <el-option
            v-for="item in pay_way_idAll"
            :key="item.id"
            :value="item.id"
            :label="item.name"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item
        v-for="item in formItemList"
        :key="item.id + item.name"
        :label="item.name + ':'"
        :prop="item.key"
      >
        <el-input
          :placeholder="`请输入${item.name}`"
          v-model="form[item.key]"
          :type="item.type == 1 ? 'input' : 'textarea'"
        ></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button @click="updateVisible(false)">取消</el-button>
      <el-button pay_way_id="primary" @click="save" :loading="loading"
        >保存
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
  name: 'PaymentAccountEdit',
  props: {
    // 弹窗是否打开
    visible: Boolean,
    // 修改回显的数据
    data: Object
  },
  data() {
    return {
      // 表单数据
      form: null,
      where: {},
      pay_way_idAll: [],
      // 表单验证规则
      rules: {
        company: [
          { required: true, message: '请输入公司名称', trigger: 'blur' }
        ],
        pay_way_id: [
          { required: true, message: '请输入收款通道', trigger: 'blur' }
        ]
      },
      // 提交状态
      formItemList: [],
      loading: false,
      // 是否是修改
      isUpdate: false
    }
  },
  created() {
    this.formItemList = []
    this.getAll()
    if (this.data) {
      let data = JSON.parse(JSON.stringify(this.data))
      let obj = {
        id: data.id,
        company: data.company,
        pay_way_id: data.pay_way_id
      }
      this.form = Object.assign({ ...JSON.parse(data.params) }, obj)
      this.isUpdate = true
    } else {
      this.form = {}
      this.isUpdate = false
    }
  },
  watch: {
    'form.pay_way_id'(val) {
      if (val) {
        this.getPaywaytemplate(val)
        if (val === this.data.pay_way_id) {
          let formcopy = {
            pay_way_id: JSON.parse(JSON.stringify(this.data)).pay_way_id,
            company: JSON.parse(JSON.stringify(this.data)).company,
            ...JSON.parse(this.data.params)
          }
          if (this.isUpdate) {
            this.$set(formcopy, 'id', this.data.id)
          }
          this.form = formcopy
        }
      }
    }
  },
  methods: {
    /* 保存编辑 */
    getAll() {
      this.$http.post('/payway/getAll').then((res) => {
        if (res.data.code === 0) {
          this.pay_way_idAll = res.data.data
        }
      })
    },
    getPaywaytemplate(pay_way_id) {
      this.$http
        .post('/paywaytemplate/index', {
          pay_way_id
        })
        .then((res) => {
          if (res.data.code === 0) {
            this.rules = {
              company: [
                { required: true, message: '请输入公司名称', trigger: 'blur' }
              ],
              pay_way_id: [
                { required: true, message: '请输入收款通道', trigger: 'blur' }
              ]
            }
            this.formItemList = res.data.data
            this.formItemList.forEach((item) => {
              this.$set(
                this.form,
                item.key,
                this.form[item.key] ? this.form[item.key] : ''
              )
              if (item.is_null == 1) {
                let obj = [
                  {
                    required: true,
                    message: `请输入${item.name}`,
                    trigger: 'blur'
                  }
                ]
                this.$set(this.rules, item.key, obj)
              }
            })
          }
        })
    },
    save() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.loading = true
          this.$http
            .post('/Paymentaccount/edit', this.form)
            .then((res) => {
              this.loading = false
              if (res.data.code === 0) {
                this.$message.success(res.data.msg)
                if (!this.isUpdate) {
                  this.form = {}
                }
                this.updateVisible(false)
                this.$emit('done')
              } else {
                this.$message.error(res.data.msg)
              }
            })
            .catch((e) => {
              this.loading = false
              this.$message.error(e.message)
            })
        } else {
          return false
        }
      })
    },
    /* 更新visible */
    updateVisible(value) {
      this.$emit('update:visible', value)
    }
  }
}
</script>

<style scoped></style>
